import {Component, OnInit} from "@angular/core";
import {Page} from "../../../public/util/page";
import {MyService} from "../my.service";
import {States} from "../../../public/setting/enums";
import {SettingUrl} from "../../../public/setting/setting_url";

@Component({
  selector: 'app-my-group-list',
  templateUrl: './my-group-list.component.html',
  styleUrls: ['./my-group-list.component.scss']
})
export class MyGroupListComponent implements OnInit {
  public searchParams: any = {};//搜索参数
  public myGroupList: Page = new Page(); //供应商
  public _loading: boolean = false;
  public routerLinks = SettingUrl.ROUTERLINK;//路由
  public states = States;       // 状态

  constructor(private myService: MyService) {
  }

  ngOnInit() {
    this.queryMyGroupList()
  }

  /**
   * 查询myGroup列表
   * @param curPage 当前页
   */
  queryMyGroupList(curPage?: number) {
    this._loading = true;
    if (curPage) this.myGroupList.curPage = curPage;//当有页码时，查询该页数据
    this.myGroupList.params = {
      curPage: this.myGroupList.curPage, //目标页码
      pageSize: this.myGroupList.pageSize, //每页条数
    };
    this.myService.getMyGroupList(this.myGroupList.params).then((res: Page) => {
      this._loading = false;
      this.myGroupList = res;
    }).catch(err => {
      this._loading = false;
    })
  }

  /**
   * 修改禁用状态
   * @param code
   * @param event:boolean
   */
  modifyMyGroupState(code, event) {
    let state = event ? States.enable : States.disable;//转换为对应的枚举
    this.myService.modifyMyGroupState(code, state).catch((res) => {
      this.queryMyGroupList();//由于switch的特殊性，因此在失败的时候刷新页面
    })
  }

  /**
   * 重置搜索
   */
  resetSearch() {
    this.searchParams = {};
    this.queryMyGroupList(1)
  }
}
